ENSAE Pierre NDIAYE de Dakar ISE1-Cycle long 2024-2025
COURS DE Statistique exploratoire spaciale avec M.Aboubacar HEMA
Travaux Pratiques N°6
Groupe : Logiciel R — Composé de Jeanne De La Flèche ONANENA AMANA, Khadidiatou COULIBALY, Tamsir NDONG, Samba DIENG

===================== CONSIGNE =================

  1. Importation et visualisation
  2. Calcul du nombre d’attaques par admin
  3. Création de rasters autour des attaques

Importation des packages et work directory setting

suppressMessages({
# Charger les bibliothèques nécessaires
library(ggplot2)
library(dplyr)
library(leaflet)
library(sf)
library(raster)
library(terra)
library(leaflet.extras)
library(viridis)
library(kableExtra)
})
## Warning: le package 'leaflet.extras' a été compilé avec la version R 4.4.2
## Setting the work directory 
knitr::opts_knit$set(root.dir = 
"C:/Users/DELL/Documents/ISEP3_2025/Stats_spatiale/Statistique-Exploratoire-Spatiale/TP6")
knitr::opts_chunk$set(fig.align = "center", fig.width = 6)

Importation et visualisation

# Charger les données depuis un fichier CSV
data <- read.csv("data/Points_data.csv")
shp <- st_read("Groupe - R/shapefiles/mli_admbnda_adm1_1m_gov_20211220.shp", quiet= TRUE)
str(data) %>% kable()
## 'data.frame':    87225 obs. of  31 variables:
##  $ event_id_cnty     : chr  "MLI33140" "BFO12659" "BFO12661" "BFO12665" ...
##  $ event_date        : chr  "04 October 2024" "04 October 2024" "04 October 2024" "04 October 2024" ...
##  $ year              : int  2024 2024 2024 2024 2024 2024 2024 2024 2024 2024 ...
##  $ time_precision    : int  1 1 1 1 1 1 1 1 2 1 ...
##  $ disorder_type     : chr  "Political violence" "Political violence" "Political violence" "Political violence" ...
##  $ event_type        : chr  "Explosions/Remote violence" "Explosions/Remote violence" "Battles" "Battles" ...
##  $ sub_event_type    : chr  "Air/drone strike" "Air/drone strike" "Armed clash" "Armed clash" ...
##  $ actor1            : chr  "Military Forces of Mali (2021-)" "Military Forces of Burkina Faso (2022-)" "JNIM: Group for Support of Islam and Muslims" "JNIM: Group for Support of Islam and Muslims" ...
##  $ assoc_actor_1     : chr  "" "" "" "" ...
##  $ inter1            : chr  "State forces" "State forces" "Rebel group" "Rebel group" ...
##  $ actor2            : chr  "Civilians (Niger)" "JNIM: Group for Support of Islam and Muslims" "Military Forces of Burkina Faso (2022-)" "VDP: Volunteer for Defense of Homeland" ...
##  $ assoc_actor_2     : chr  "Miners (Niger)" "" "VDP: Volunteer for Defense of Homeland" "" ...
##  $ inter2            : chr  "Civilians" "Rebel group" "State forces" "Identity militia" ...
##  $ interaction       : chr  "State forces-Civilians" "State forces-Rebel group" "State forces-Rebel group" "Rebel group-Identity militia" ...
##  $ civilian_targeting: chr  "Civilian targeting" "" "" "" ...
##  $ iso               : int  466 854 854 854 854 854 288 466 566 566 ...
##  $ region            : chr  "Western Africa" "Western Africa" "Western Africa" "Western Africa" ...
##  $ country           : chr  "Mali" "Burkina Faso" "Burkina Faso" "Burkina Faso" ...
##  $ admin1            : chr  "Kidal" "Cascades" "Sahel" "Nord" ...
##  $ admin2            : chr  "Abeibara" "Comoe" "Soum" "Yatenga" ...
##  $ admin3            : chr  "Tinzawatene" "Ouo" "Djibo" "Ouahigouya" ...
##  $ location          : chr  "Zakak" "Dida Forest" "Djibo" "Aorema" ...
##  $ latitude          : num  19.6 10 14.1 13.7 12.7 ...
##  $ longitude         : num  2.891 -4.029 -1.642 -2.334 -0.131 ...
##  $ geo_precision     : int  1 2 2 1 2 1 1 1 2 1 ...
##  $ source            : chr  "FAMAMali; Twitter; Undisclosed Source" "Undisclosed Source" "Al Zallaqa; Whatsapp" "Whatsapp" ...
##  $ source_scale      : chr  "Local partner-Other" "Local partner-Other" "New media" "New media" ...
##  $ notes             : chr  "On 4 October 2024, the Malian air force carried out an airstrike against a convoy in the village of Zakak (Abei"| __truncated__ "On 4 October 2024, the Burkinabe force carried airstrikes against JNIM militants in the Dida Forest (Ouo, Comoe"| __truncated__ "On 4 October 2024, JNIM militants carried out several attacks against positions of Burkinabe force and voluntee"| __truncated__ "On 4 October 2024, JNIM militants attacked a position of volunteer fighters (VDP) in the village of Aorema (Oua"| __truncated__ ...
##  $ fatalities        : int  7 5 0 0 0 0 0 3 0 2 ...
##  $ tags              : chr  "" "" "" "" ...
##  $ timestamp         : int  1728335020 1728358478 1728358478 1728358478 1728358478 1728358478 1728358486 1728358502 1728358504 1728358504 ...
# Voyons les noms des variables
colnames(data)
##  [1] "event_id_cnty"      "event_date"         "year"              
##  [4] "time_precision"     "disorder_type"      "event_type"        
##  [7] "sub_event_type"     "actor1"             "assoc_actor_1"     
## [10] "inter1"             "actor2"             "assoc_actor_2"     
## [13] "inter2"             "interaction"        "civilian_targeting"
## [16] "iso"                "region"             "country"           
## [19] "admin1"             "admin2"             "admin3"            
## [22] "location"           "latitude"           "longitude"         
## [25] "geo_precision"      "source"             "source_scale"      
## [28] "notes"              "fatalities"         "tags"              
## [31] "timestamp"
# VOYONS LES PAYS
unique(data$country)
##  [1] "Mali"          "Burkina Faso"  "Ghana"         "Nigeria"      
##  [5] "Benin"         "Guinea"        "Senegal"       "Ivory Coast"  
##  [9] "Guinea-Bissau" "Mauritania"    "Niger"         "Togo"         
## [13] "Liberia"       "Cape Verde"    "Gambia"        "Sierra Leone"
# Convertir les données en un objet spatial sf
data_spatial <- sf::st_as_sf(data, coords = c("longitude", "latitude"), crs = st_crs(shp))
## Voir cela sur leaflet...
# Visualisation pour les différents pays
ggplot(data_spatial) +
    geom_sf(fill = NA, color = "blue", size = 0.5) +
  aes(colour = country) +
  geom_sf(size = 1.2) +
  scale_fill_hue(direction = 1) +
  scale_color_hue(direction = 1)

## Palette de couleurs
country_palette <- country_palette <- colorFactor(viridis(length(unique(data$country)), 
                                            option = "turbo"), domain = data$country)


# Créer une carte interactive
leaflet() %>%
  addTiles() %>%  # Couche de base (OpenStreetMap)

  # Ajouter les limites  (administration de niveau 0 - national)
  addPolygons(data = shp, color = "brown", weight = 2, opacity = 1, fillOpacity = 0.5,
              popup = ~ADM1_FR) %>%  # Afficher l'information dans une popup

  # Ajouter les points d'événements (assurez-vous que AOI_event est un objet sf avec un CRS défini)
  addCircleMarkers(data = data, weight = 0.1, opacity = 2, fillOpacity = 1.4,
                  radius = 1.5,  # Adjust circle size
                   color = ~country_palette(country)) %>%
    addLegend("bottomright", pal = country_palette, values = data$country,
            title = "Evenements par pays", opacity = 1) %>%
    addResetMapButton()%>%  # Recentrer la carte
    addFullscreenControl()  #ajout du basculement en mode plein écran
## Assuming "longitude" and "latitude" are longitude and latitude, respectively

On choisit notre zone d’intérêt

# On selectionne notre pays (Area of interest)
AOI = "Mali"
# Filtrer pour le Sénégal
AOI_event <- data_spatial %>%
  filter(country == AOI)

# Visualisation des événements au Sénégal
ggplot(AOI_event) +
  aes(fill = event_type, colour = event_type) +
  geom_sf(size = 1.2) +
  scale_fill_hue(direction = 1) +
  theme_minimal()

## Palette de couleurs
event_palette <- colorFactor(palette = "Set2", domain = AOI_event$event_type)

# Créer une carte interactive
leaflet() %>%
  addTiles() %>%  # Couche de base (OpenStreetMap)

  # Ajouter les limites  (administration de niveau 0 - national)
  addPolygons(data = shp, color = "brown", weight = 2, opacity = 1, fillOpacity = 0.5,
              popup = ~ADM1_FR) %>%  # Afficher l'information dans une popup

  # Ajouter les points d'événements (assurez-vous que AOI_event est un objet sf avec un CRS défini)
  addCircleMarkers(data = AOI_event, weight = 0.1, opacity = 2, fillOpacity = 1.4,
                  radius = 2,  # Adjust circle size
                   color = ~event_palette(event_type)) %>%
    addLegend("bottomright", pal = event_palette, values = AOI_event$event_type,
            title = "Event Type", opacity = 1) %>%
    addResetMapButton()%>%  # Recentrer la carte
    addFullscreenControl()  #ajout du basculement en mode plein écran
## 2. Calcul du nombre d’évènements par admin (0-3)
# On joint les deux
points_ml<- st_join(data_spatial, shp, join = st_intersects)
points_ml %>% data.frame() %>% tail(5) %>% kable()
event_id_cnty event_date year time_precision disorder_type event_type sub_event_type actor1 assoc_actor_1 inter1 actor2 assoc_actor_2 inter2 interaction civilian_targeting iso region country admin1 admin2 admin3 location geo_precision source source_scale notes fatalities tags timestamp Shape_Leng Shape_Area ADM1_FR ADM1_PCODE ADM1_REF ADM1ALT1FR ADM1ALT2FR ADM0_FR ADM0_PCODE date validOn validTo geometry
87221 SIE2 01 January 1997 1997 3 Political violence Battles Government regains territory Military Forces of Sierra Leone (1996-1997) State forces RUF: Revolutionary United Front Rebel group State forces-Rebel group 694 Western Africa Sierra Leone Eastern Kono Gbane Mandu 2 No Peace Without Justice; SL-LED Local partner-New media Around 1 January 1997 (month of), Military Forces of Sierra Leone (1996-1997) and RUF: Revolutionary United Front engaged in battles in Mandu (Eastern, Kono). No fatalities. 0 1670286851 NA NA NA NA NA NA NA NA NA NA NA NA POINT (-10.9332 8.4642)
87222 SIE3 01 January 1997 1997 3 Political violence Battles Armed clash Kamajor Militia Political militia RUF: Revolutionary United Front Rebel group Rebel group-Political militia 694 Western Africa Sierra Leone Southern Bo Selenga Selenga 2 No Peace Without Justice; SL-LED Local partner-New media Around 1 January 1997 (month of), Kamajor Militia and RUF: Revolutionary United Front engaged in battles in Selenga (Southern, Bo). No fatalities. 0 1670286851 NA NA NA NA NA NA NA NA NA NA NA NA POINT (-11.7047 8.1221)
87223 SIE6 01 January 1997 1997 3 Political violence Violence against civilians Attack Military Forces of Sierra Leone (1996-1997) State forces Civilians (Sierra Leone) Civilians State forces-Civilians Civilian targeting 694 Western Africa Sierra Leone Southern Bonthe Imperi York Island 2 No Peace Without Justice; SL-LED Local partner-New media Around 1 January 1997 (month of), Military Forces of Sierra Leone (1996-1997) engaged in violence against civilians in York Island (Southern, Bonthe). No fatalities. 0 1670286851 NA NA NA NA NA NA NA NA NA NA NA NA POINT (-12.4694 7.5317)
87224 SIE7 01 January 1997 1997 3 Political violence Battles Armed clash Kamajor Militia Political militia Military Forces of Sierra Leone (1996-1997) State forces State forces-Political militia 694 Western Africa Sierra Leone Southern Moyamba Fakunya Fakunya 2 SL-LED; No Peace Without Justice Local partner-New media Around 1 January 1997 (month of), Kamajor Militia and Military Forces of Sierra Leone (1996-1997) engaged in battles in Fakunya (Southern, Moyamba). No fatalities. 0 1670286851 NA NA NA NA NA NA NA NA NA NA NA NA POINT (-12.338 8.231)
87225 SIE8 01 January 1997 1997 3 Strategic developments Strategic developments Headquarters or base established Military Forces of Sierra Leone (1996-1997) State forces State forces only 694 Western Africa Sierra Leone Southern Moyamba Kori Taiama 2 No Peace Without Justice; SL-LED Local partner-New media base 0 1678830926 NA NA NA NA NA NA NA NA NA NA NA NA POINT (-12.06 8.2013)
# On garde les points du Mali
points_ml <- points_ml %>% filter(!is.na(ADM1_PCODE))
points_ml %>% data.frame() %>% dim()
## [1] 11547    42
## Différence de 6 points...
dim(AOI_event)
## [1] 11541    30

Nombre d’évènements par région…méthode 1

Dans ce cas, on a utilisé l’objet point_counts pour calculer le nombre d’attaques par admin. Si cet objet avait été créé avec le shapefile d’une autre division administrative, il permettrait de faire la même chose pour cette division.

Une idée serait de créer ici une fonction.

# Compter le nombre de points par région
point_counts <- points_ml %>%
  group_by(ADM1_FR) %>%  #  groupper par régions
  summarise(Nombre_attaques = n())

point_counts %>%
 st_drop_geometry() %>%
data.frame() %>% kable()
ADM1_FR Nombre_attaques
Bamako 446
Gao 1956
Kayes 265
Kidal 935
Koulikoro 429
Menaka 720
Mopti 3852
Sikasso 308
Ségou 1380
Tombouctou 1256
t1 <-table(AOI_event$admin1, AOI_event$event_type) 
kable(t1)
Battles Explosions/Remote violence Protests Riots Strategic developments Violence against civilians
Bamako 31 6 245 65 58 41
Gao 476 251 78 30 444 679
Kayes 79 4 75 28 28 51
Kidal 237 368 44 27 135 124
Koulikoro 132 54 40 26 72 103
Menaka 211 93 13 7 137 246
Mopti 1095 521 105 39 599 1498
Segou 410 199 35 14 231 494
Sikasso 79 25 44 44 58 58
Tombouctou 300 216 59 44 225 411
t2 <- table(AOI_event$admin2)  %>% data.frame() 
colnames(t2) <- c("division", "nb_events")
head(t2,10) %>% kable()
division nb_events
Abeibara 117
Anderamboukane 109
Ansongo 824
Bafoulabe 32
Bamako 446
Banamba 52
Bandiagara 763
Bankass 481
Baraoueli 12
Bla 14
t3 <- table(AOI_event$admin3)  %>% data.frame()
colnames(t3) <- c("division", "nb_events")
head(t3, 10) %>% kable()
division nb_events
Abeibara 49
Adjelhoc 136
Alafia 44
Anchawadi 96
Anderamboukane 110
Anefif 57
Ansongo 164
Arham 1
Bafoulabe 11
Baguindabougou 1
# Reprojeter pour obtenir des unités en mètres
AOI_prj <- st_transform(AOI_event, crs = 32629)  # EPSG 32629 pour UTM zone 29N -- pour le Mali

# Définir l'étendue (extent) et la résolution en mètres
ext <- raster::extent(sf::st_bbox(AOI_prj)) #extent
res <- 5000  # Résolution de 5 km
rast_crs <- CRS("+proj=utm +zone=29 +datum=WGS84 +units=m +no_defs")

raster_template <- raster::raster(ext=ext, resolution=res, crs=rast_crs)

# Evènements...
AOI_Raster <- raster::rasterize(AOI_prj,raster_template,field=1, fun= sum)

#Save with GTIFF format
raster::writeRaster(AOI_Raster, filename = "Rasterisation.tif", format = "GTiff", overwrite = TRUE)
## Palette de couleurs
pal <- colorNumeric(palette = viridis(1000, option = "viridis"), 
                    domain = raster::values(AOI_Raster),
                    na.color = "transparent")

# Créer une carte interactive
leaflet() %>%
  addTiles() %>%  # Couche de base (OpenStreetMap)

 addPolygons(data = shp, color = "brown", weight = 2, opacity = 0.2, fillOpacity = 0.1,
            popup = ~ADM1_FR) %>%  

  addRasterImage(AOI_Raster, opacity = 2,colors= pal) %>%
   addLegend(pal = pal, values = raster::values(AOI_Raster),
    title = "Nombre d'événement") %>%
    addResetMapButton() %>%  # Recentrer la carte
    addFullscreenControl()  #ajout du basculement en mode plein écran
## Warning in colors(.): Some values were outside the color scale and will be
## treated as NA
summary(raster::values(AOI_Raster)) 
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's 
##     1.0     1.0     2.0     5.8     4.5   445.0   94959